package com.example.systemserver.Service;

import cn.hutool.core.util.IdUtil;
import com.example.systemserver.Bean.BookBean;
import com.example.systemserver.Bean.RequestBean;
import com.example.systemserver.Dao.BookMapper;
import com.example.systemserver.config.MyException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class BookService {

    @Resource
    private BookMapper bookMapper;

    public void save(BookBean bookBean) {
        log.debug("图书信息保存,检查参数完整度");
        if (bookBean == null) {
            throw new MyException("参数错误");
        }
        if (bookBean.getTitle() == null || bookBean.getTitle().trim().isEmpty()) {
            throw new MyException("书名不能为空");
        }
        if (bookBean.getAuthor() == null || bookBean.getAuthor().trim().isEmpty()) {
            throw new MyException("作者不能为空");
        }

        // 检查书名是否重复
        BookBean existingBook = bookMapper.findByTitle(bookBean.getTitle().trim());
        if (bookBean.getId() == null) {
            // 新增图书时检查书名是否已存在
            if (existingBook != null) {
                throw new MyException("书名已存在，添加失败");
            }
        } else {
            // 更新图书时检查书名是否被其他图书占用
            if (existingBook != null && !existingBook.getId().equals(bookBean.getId())) {
                throw new MyException("书名已存在，更新失败");
            }
        }

        log.debug("图书信息保存,判断新增/更新");
        if (bookBean.getId() == null) {
            log.debug("新增，补充参数");
            bookBean.setId(IdUtil.getSnowflakeNextId());
            bookBean.setCreatedTime(System.currentTimeMillis());
            bookBean.setUpdatedTime(System.currentTimeMillis());

            log.debug("新增，执行操作");
            int updated = bookMapper.save(bookBean);
            if (updated <= 0) {
                throw new MyException("保存失败");
            }
        } else {
            log.debug("更新，补充参数");
            bookBean.setUpdatedTime(System.currentTimeMillis());
            log.debug("更新，执行操作");
            int updated = bookMapper.update(bookBean);
            if (updated <= 0) {
                throw new MyException("更新失败");
            }
        }
    }

    @SuppressWarnings("unchecked")
    public PageInfo<BookBean> list(RequestBean requestBean) {
        log.debug("查询列表,设置分页");
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize(), requestBean.getOrderBy());
        log.debug("查询列表,执行操作");

        // 创建一个新的BookBean实例用于查询参数
        BookBean params = new BookBean();
        if (requestBean.getParams() != null) {
            // 确保params是Map类型
            if (requestBean.getParams() instanceof Map) {
                Map<String, Object> paramsMap = (Map<String, Object>) requestBean.getParams();

                // 从requestBean的params中提取图书相关的查询条件
                Object titleObj = paramsMap.get("title");
                if (titleObj != null) {
                    params.setTitle(titleObj.toString());
                }

                Object authorObj = paramsMap.get("author");
                if (authorObj != null) {
                    params.setAuthor(authorObj.toString());
                }
            }
        }

        List<BookBean> list = bookMapper.list(params);

        return PageInfo.of(list);
    }

    public boolean remove(BookBean bookBean) {
        int updated = bookMapper.remove(bookBean);
        return updated > 0;
    }

    // 批量删除方法
    public int removeBatch(List<Long> ids) {
        int count = 0;
        for (Long id : ids) {
            BookBean bookBean = new BookBean();
            bookBean.setId(id);
            if (remove(bookBean)) {
                count++;
            }
        }
        return count;
    }

    // 全量删除方法
    public int removeAll() {
        return bookMapper.removeAll();
    }

    public BookBean info(BookBean bookBean) {
        BookBean info = bookMapper.info(bookBean);
        return info;
    }
}
